<?php
namespace App\Http\Controllers\Company;

use App\Models\Questions;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\DB;
/**
 *
 * @author Joker
 *        
 */
class QuestionController extends CompanyController
{
    
    public function index()
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        $questions = Questions::where('company_id','=',$this->userinfo->company_id)->paginate(20);
        return view('company.question')
                ->with('questions', $questions);
    }
    
    public function create()
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        return view('company.questiondetails');
    }
    
    public function show($id)
    {
        if(empty($this->userinfo->company_id))
            return redirect(url('/company/center/companyinfo-default'));
        
        $question = Questions::where(['id'=>$id,'company_id'=>$this->userinfo->company_id])->first();
        if(empty($question))
            return abort(404);
        
        return view('company.questiondetails')->with('result', $question);
    }
    
    public function store(Request $request)
    {
        $this->validate($request, [
            'title'                 => 'required|max:255',
            'options'               => 'required',
            'answer'                => 'required',
            'questions_num'         => 'required|integer',
            'explain'               => 'required'
        ]);
        
        $company_id = $request->get('questions_id', 0);
        $post = array_except($request->all(), ['_token', 'questions_id']);
        $post['company_id'] = $this->userinfo->company_id;
        try
        {
            if($company_id > 0 AND !empty($post)) {
                $company_quest = Questions::find($company_id);
                foreach ($post as $key => $val) {
                    $company_quest->$key = $val;
                }
                $company_quest->save();
            } else{
                $company_quest = Questions::firstOrCreate($post);
            }
        }catch (\PDOException $e){
            Log::info(['企业试题创建失败'=>$e->getMessage()]);
            return $this->formatJson('error','试题创建失败！');
        }
        return $this->formatJson('success','试题创建成功！',$company_quest,url('/company/center/questions'));
    }
    
    public function destroy($question_id)
    {
    
        $quest = Questions::where(['id'=>$question_id,'company_id'=>$this->userinfo->company_id])->first();
        $inExam = DB::table('examination_question')->where('question_id','=',$question_id)->get();
        if(!empty($inExam))
            return $this->formatJson('error','已有试卷关联到该试题，若想删除，请先删除关联！');
        if ($quest->delete())
            return $this->formatJson('success','题目 ['. str_limit($quest->title, 15) . '] 删除成功!');
        else
            return  $this->formatJson('error','删除失败！');
    }
}